package com.sita.tboard.ui.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.sita.bike.R;
import com.sita.bike.persistence.Route;
import com.sita.bike.persistence.Tracks;
import com.sita.bike.utils.PersistUtils;
import com.sita.tboard.ui.adapter.RoadMonthAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

//import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class RoadRecordFragment extends Fragment implements OnGeocodeSearchListener {
    private static RoadRecordFragment fragment;
    public ListView listView_month;
    public ArrayList<String> month;
    private long timeStart, timeEnd;
    private GeocodeSearch geocoderSearch;
    private String streetName;
    private List<Route> monthTracks;
    RoadMonthAdapter roadmonthAdapter;
    private ImageView road_record_menu;

    public static RoadRecordFragment getInstance() {
        if (fragment == null) {
            synchronized (RoadRecordFragment.class) {
                if (fragment == null) {
                    fragment = new RoadRecordFragment();
                }
            }
        }
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_road_record, null);
        listView_month = (ListView) view.findViewById(R.id.road_record_list_month);
        month = new ArrayList<>();
        monthTracks = new ArrayList<>();

        roadmonthAdapter = new RoadMonthAdapter(getActivity(), month, monthTracks);
        listView_month.setAdapter(roadmonthAdapter);
        //初始化地理编码服务
        geocoderSearch = new GeocodeSearch(getActivity());
        geocoderSearch.setOnGeocodeSearchListener(this);

        UpdateDataTask updateTextTask = new UpdateDataTask();
        updateTextTask.execute();

        road_record_menu = (ImageView) view.findViewById(R.id.road_record_menu);
        road_record_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                SlidingFragmentActivity s = (SlidingFragmentActivity) getActivity();
//                s.getSlidingMenu().toggle();
            }
        });

        return view;
    }

    /**
     * 响应逆地理编码
     */
    public void getAddress(final LatLonPoint latLonPoint) {
        RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200,
                GeocodeSearch.AMAP);// 第一个参数表示一个Latlng，第二参数表示范围多少米，第三个参数表示是火系坐标系还是GPS原生坐标系
        geocoderSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求
    }

    /**
     * 逆地理编码回调
     */
    @Override
    public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
        if (rCode == 0) {
            if (result != null && result.getRegeocodeAddress() != null
                    && result.getRegeocodeAddress().getFormatAddress() != null) {
                streetName = result.getRegeocodeAddress().getStreetNumber().getStreet();

            }
        } else if (rCode == 27) {


        } else if (rCode == 32) {

        } else {

        }
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
    }


    private class UpdateDataTask extends AsyncTask<String, Integer, String> {
        public UpdateDataTask() {
            super();
        }

        @Override
        protected String doInBackground(String... params) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                timeStart = sdf.parse("2016-01-01 00:00:00").getTime();
                timeEnd = System.currentTimeMillis();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            monthTracks = PersistUtils.getTimeSelectRoutes(timeStart, timeEnd);//一月份的全部路径
            if (monthTracks.size() != 0) {
                month.add("1月汇总");
            } else {
                month.add("暂无路线");
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            roadmonthAdapter.refresh(month, monthTracks);
        }
    }
}
